package com.lowkey.dataSource;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.sql.DataSource;

import com.lowkey.cache.CacheThread;
import com.lowkey.tools.Constant;
import com.lowkey.tools.XMLFactory;

public class DBControl {
	private static Map<String,String> DBMAP=null;
	private Connection conn=null;
	private static DataSource dataSource = null;
	static {
		load();
	}
	
	private static void load(){
		if(null==DBMAP){
			XMLFactory.load("/lowkey.datasource.xml");
			DBMAP=Constant.DBMAP;
			CacheThread ct = new CacheThread();
			Thread t = new Thread(ct);
			t.start();
		}
	}
	
	@SuppressWarnings("unchecked")
	public DBControl(){
		if(null==DBMAP){
			load();
		}else{
			if(DBMAP.containsKey("dataSource")){
				if(DBMAP.get("dataSource")==null){
					//DBMAP获得信息,再注入至dataSource中.
					try {
						Class dataSourceClass = Class.forName(DBMAP.get("dataSource"));
						Set<String> set=DBMAP.keySet();
						Iterator<String> it = set.iterator();
						dataSource = (DataSource) dataSourceClass.newInstance();
						while(it.hasNext()){
							String key = it.next();
							if(!key.equals("dataSource")){
								Method dataSourceClassMethod = 
										dataSourceClass.getDeclaredMethod(
												"set"+key.substring(0,1).toUpperCase()+key.substring(1), String.class);
								dataSourceClassMethod.invoke(dataSource,DBMAP.get(key));
							}
						}
					}
					//[start]异常区域
					catch (ClassNotFoundException e) {
						e.printStackTrace();
					} catch (InstantiationException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IllegalArgumentException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (SecurityException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (NoSuchMethodException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}  
					//[end]
					getDataSource();
				}else{
					getDataSource();
				}
			}else{
				getSimpleConn();
			}
		}
	}
	//获得简单的连接,并完成
	public void getSimpleConn(){
		try {
			Class.forName(DBMAP.get("driver"));
			conn = DriverManager.getConnection(DBMAP.get("url"), DBMAP.get("username"), DBMAP.get("password"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//获得来自dataSource的连接.
	public Connection getDataSource(){
		if(dataSource!=null){
			try {
				conn = dataSource.getConnection();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return conn;
	}
	//处理传送过来的sql语句
	public void setData(String sql){
		Statement stmt;
		try {
			stmt = conn.createStatement();
			stmt.execute(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	//返回结果集的方法:
	public ResultSet getData(String sql){
		ResultSet rs = null;
			Statement stmt;
			try {
				stmt = conn.createStatement();
				rs=stmt.executeQuery(sql);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}  
		return rs;
	}
	
	
	//关闭连接的方法 
	public void close(){
		try {
			if(conn!=null||!conn.isClosed()){
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
}
